﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace QuanLyQuangCao
{
    public partial class frmGD_QC_Edit : Form
    {
        public string pMaLoai = "";
        public string pGhiChu = "";
        public int pGia = 0;
        public frmGD_QC_Edit()
        {
            InitializeComponent();
        }
        private void frmQuangCao_Sua_Load(object sender, EventArgs e)
        {
            prvdefaulvalue();
            if (pMaLoai == "")
                txtMaLoai.Enabled = true;
            else
            {
                txtMaLoai.Enabled = false;
                prvSetData();
            }

        }
       // private DataSet ds = new DataSet();

        private void btnDongY_Click(object sender, EventArgs e)
        {

            if (pMaLoai == "")
            {
                try
                {
                    string ml = txtMaLoai.Text.Trim();
                    string gc = txtGhiChu.Text.Trim();
                    int gmk = Convert.ToInt32(txtGiaMotKy.Text.Trim());
                    string sql = "insert into LoaiQuangCao(ma_loai,ghi_chu,gia_mot_ky)" + "values ('" + ml + "','" + gc + "','" + gmk + "')";
                    SqlConnection sc = new SqlConnection(frmChinh.me.sqlcon);
                    SqlCommand sqlCmd = new SqlCommand(sql, sc);
                    sc.Open();
                    sqlCmd.ExecuteNonQuery();
                    sc.Close();
                    MessageBox.Show("Bạn Đã Thêm Thành Công", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    this.Close();
                }
                catch
                {
                    MessageBox.Show("Ma da ton tai vui long nhap ma khac");
                }
            }
            else
            {
                string tsGhiChu = txtGhiChu.Text.Trim();
                string tsGia = txtGiaMotKy.Text.Trim();
                string sql = "update LoaiQuangCao" + " set ghi_chu ='" + tsGhiChu + "'," + "Gia_Mot_Ky = " + "'" + tsGia + "'"
                    + "where Ma_Loai= '" + pMaLoai + "'";
                SqlConnection sc = new SqlConnection(frmChinh.me.sqlcon);
                SqlCommand sqlCmd = new SqlCommand(sql, sc);
                sc.Open();
                sqlCmd.ExecuteNonQuery();
                sc.Close();
                MessageBox.Show("Bạn Đã Cập Nhật Thành Công", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.Close();

            }
        }
        private void prvSetData()
        {
            txtMaLoai.Text = pMaLoai;
            txtGhiChu.Text = pGhiChu;
            txtGiaMotKy.Text = pGia.ToString();
        }
        private void prvdefaulvalue()
        {
            txtMaLoai.Text = "";
            txtGhiChu.Text = "";
            txtGiaMotKy.Text = "";
            txtMaLoai.Focus();
        }
    }
}
